package tut.collection.code;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class MyCollections {

	public static void main(String args[]) {
		System.out.println();
		List li1 = new ArrayList();
		li1.add("one");
		li1.add("two");

		Set s = new HashSet();
		s.add("s one");
		s.add("s two");

		// collection iterator
		MyCollections.doSomeThing(li1);
		MyCollections.doSomeThing(s);

		// return value of add method and changed of collection after add
		System.out.println(s.add("s one"));
		System.out.println(s.add("new"));
		System.out.println(li1.add("one"));
		System.out.println(li1.add("new"));
		printCollection(s);

		String oneLi = "one";
		String notInLi = "not in li";
		String oneSet = "s one";
		String notInSet = "not in set";

		printCollection(li1);
		printCollection(s);

		//return value of remove method
		System.out.println("\n=============");
		System.out.println(li1.remove(oneLi));
		System.out.println(li1.remove(notInLi));
		System.out.println(s.remove(oneSet));
		System.out.println(s.remove(notInSet));

		// add, remove collections of object. difference of add and addAll
		List aList = new ArrayList();
		aList.add("li one");
		aList.add("li two");

		Set aSet = new HashSet();
		aSet.add("s one");

		Collection collection = new HashSet();
		collection.add(aList);
		collection.add(aSet);

		Collection useAddAllCollection = new HashSet();
		useAddAllCollection.addAll(aList);
		useAddAllCollection.addAll(aSet);

		MyCollections.printCollection(collection);
		System.out.println(collection.size());
		MyCollections.printCollection(useAddAllCollection);
		System.out.println(useAddAllCollection.size());

		// removeAll, retainAll => tested: not safe
		System.out.println("\n=======removelAll, retainAll======\n");
		Collection colA = new HashSet();
		colA.add("a1");
		colA.add("a2");

		Collection colB = new ArrayList();
		colB.add("b1");
		colB.add("b2");

		Collection colC = new ArrayList();
		colB.add("c1");
		colB.add("c2");

		Collection targetColl = new HashSet();
		targetColl.addAll(colA);
		targetColl.addAll(colB);
		targetColl.addAll(colC); // target now contains [a1,a2,b1,b2,c1,c2]
		System.out.println("targetColl.addAll colA, colB, colC");
		printCollection(targetColl);

		System.out.println("targetColl.removeAll(colA);");
		targetColl.removeAll(colA);	// target now contains[b1,b2,c1,c2]
		printCollection(targetColl);
/*
		targetColl.retainAll(colB); // target now contains [b1,b2]
		System.out.println("targetColl.retainAll(colB);");
		printCollection(targetColl);

		targetColl.removeAll(colC); // target now contains [b1,b2]
		System.out.println("targetColl.removeAll(colC);");
		printCollection(targetColl);

		targetColl.removeAll(colB); // collection empty
		System.out.println("targetColl.removeAll(colB);");
		printCollection(targetColl);
*/
		// removeAll, retainAll => tested: not safe

		boolean contaisElement = targetColl.contains("b1");
		boolean containsAll = targetColl.containsAll(colB);
		boolean containsAll2 = targetColl.containsAll(colC);
		System.out.println(contaisElement);
		System.out.println(containsAll);
		System.out.println(containsAll2);
		targetColl.removeAll(colC);

		printCollection(targetColl);

	}

	public static void doSomeThing(Collection collection) {
		System.out.println("\n=============");
		Iterator i = collection.iterator();
		while(i.hasNext()) {
			Object ob = i.next();
			System.out.println(ob);
			System.out.println(ob.getClass().getName());
		}
		System.out.println("----------");
		for (Object o : collection) {
			System.out.println(o);
		}

		System.out.println();
	}

	public static void printCollection(Collection col) {
		System.out.println("\n------print collection------");
		Iterator i = col.iterator();
		while (i.hasNext()) {
			Object object = (Object) i.next();
			System.out.println(object);
		}
		System.out.println();
	}

/*
	=> summary
	return off Add, remove
	addAll()
	containsAll()
	contains()
	Iterator i = col.iterator();
	while(i.hashNext){
		Object o = (Object) i.next();
	}
	or
	for(Object o: col){
	}

*/
}
